<?php
namespace app\yunda\admin;

use app\common\builder\ZBuilder;
use app\yunda\model\Map as MapModel;
use think\Db;

/**
 * Class Map快递网点
 * @package app\yunda\admin
 */
class Map extends Base{
    public function index()
    {
        // 获取查询和排序规则
        $map = $this->getMap();
        $order = $this->getOrder();
        $data = Db::table('dp_yd_map',true)
            // ->view('xiaoan_post',['title'],'xiaoan_post.id=xiaoan_comment.post_id')
            ->where($map)
            ->order($order)
            ->order('id desc')
            ->paginate()->each(function($item){

                return $item;
            });
        return ZBuilder::make('table')
            ->setTableName('yd_map')  //需要修改 这里修改正确可以直接修改字段 删除
            ->addColumns([
                ['id','id'],
                ['wd_name','网点名称'],
                ['wd_address','网点地址'],
                ['fw_time','服务时间'],
                ['xd_phone','下单电话'],
                ['ts_phone','投诉电话'],
                ['cx_phone','查询电话'],
                ['ps_area','配送范围'],
                ['yw_else','增值业务'],
                ['create_time','创建时间','datetime'],
                ['right_button', '操作', 'btn']
            ])
            ->addTopButton('add', [],[])
            ->addRightButton('edit',[],[])
            ->addTopButtons(['delete'])
            ->setRowList($data)
            //->addFilter(['title' => 'admin_action', 'username'])
            ->setSearch(['id' => 'ID', 'good_name' => '商品名称'])
            ->fetch();
    }
    public function add(){
        if($this->request->isPost()){
            // 第一步 获取数据 如果后台面向大众 需要用only方法
            $data = request()->post();
            $data['wd_address']=$data['map_address'];
            $data['map']=explode(',',$data['map']);
            $data['longitude']=$data['map'][0];
            $data['latitude']=$data['map'][1];
            // 第二部 验证数据
            $result = $this->validate($data,'Map');
            if($result!==true) $this->error($result);

//            halt($data);
            // 第三部 存储
            MapModel::create($data);
            $this->success('保存成功', null, '_parent_reload');
        }
        return ZBuilder::make('form')
            ->addFormItems([
                ['text','wd_name','网点名称'],
//                ['text','wd_address','网点地址'],
                ['bmap','map','网点地址','LDW8VplRGbRP6RDP8KTtqdK9zfeQduEn'],
                ['text','fw_time','服务时间','必须,格式00:00-24:00'],
                ['text','xd_phone','下单电话'],
                ['text','ts_phone','投诉电话'],
                ['text','cx_phone','查询电话'],
                ['text','ps_area','配送范围'],
                ['textarea','yw_else','增值业务'],
            ])
            ->fetch();
    }
    public function edit($id=null){
        if($this->request->isPost()){
            $data = request()->post();
            $data['wd_address']=$data['map_address'];
            $data['map']=explode(',',$data['map']);
            $data['longitude']=$data['map'][0];
            $data['latitude']=$data['map'][1];
            $result = $this->validate($data,'Map');
            if($result!==true) $this->error($result);
//            halt($data);
            MapModel::update($data);
            $this->success('保存成功', null, '_parent_reload');
        }
        $data = MapModel::get($id);
        $data['map_address']=$data['wd_address'];
        $data['map']=$data['longitude'].','.$data['latitude'];
        return ZBuilder::make('form')
            ->addFormItems([
                ['hidden','id'],  // 多增加一个影藏ID 即可
                ['text','wd_name','网点名称'],
                //['text','wd_address','网点地址'],
                ['bmap','map','网点地址','LDW8VplRGbRP6RDP8KTtqdK9zfeQduEn'],
                ['text','fw_time','服务时间','必须,格式00:00-24:00'],
                ['text','xd_phone','下单电话','必须'],
                ['text','ts_phone','投诉电话','必须'],
                ['text','cx_phone','查询电话','必须'],
                ['text','ps_area','配送范围','必须'],
                ['textarea','yw_else','增值业务'],
            ])
            ->setFormData($data)
            ->fetch();
    }
}